package com.mk.common.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
import org.apache.commons.lang3.StringUtils;

import org.junit.Test;

import com.mk.common.model.ReasonBO;
/**
 * Optional类
 * @author 上官雅晴
 *
 */
public class OptionalDemo {

	@Test
	public void test1() {
		String res = "";
		System.out.println( Optional.ofNullable(res).orElseGet(()->"无") ); // ""
	}
	
	@Test
	public void test2() {
		String result = "4";
		String res = Optional.ofNullable(result).filter(t->StringUtils.isNotBlank(t)).map(u->u+"12").orElseGet(()->"无");
		System.out.println(res);
	
	}
	
	@Test
	public void test3() {
		Map<String, Object> param = new HashMap<>();
		param.put("KHLX", 175);
		param.put("JCJB", "");
		param.put("KSRQ", "2021-09-11");
		param.put("JSRQ", "2021-10-01");
		
		String res = Optional.ofNullable(param.get("JCJB"))
//				.map(u->u.get("JCJB"))
				.map(u->String.valueOf(u))
				.filter(t->org.apache.commons.lang3.StringUtils.isNotBlank(t))
				.orElseGet(()->"无");
		
//		String JCJB = Optional.ofNullable(param.get("JCJ")).orElse("暂无").toString();
		
		System.out.println(res);
	}
	
	@Test
	public void test4() {
		Map<String, Object> param = new HashMap<>();
		param.put("KHLX", "175");
		param.put("JCJB", 3);
		param.put("KSRQ", "2021-09-11");
		param.put("JSRQ", "2021-10-01");
		
		String checkLevel = 
				Optional.ofNullable(param.get("JCJB2")).orElse("100.00").toString();
		System.out.println(checkLevel);
	}
	
	@Test
	public void test5() {
		String KHXM = "  "; // orElse
		String khxm = Optional.ofNullable(KHXM).orElse("").toString();
		
		System.out.println( khxm );
	}
	
	@Test
	public void test6() {
		String val = null;
		String val2 = "";

		System.out.println( Optional.ofNullable(val).isPresent() ); //false
		System.out.println( Optional.ofNullable(val2).isPresent() );//true
	}
	
	@Test
	public void test7() {
		/**
		 * 为空-"" || null || "   "
		 * "".split(",");
		 */
		String val = "";
		if( "".equals(Optional.ofNullable(val).orElse("").trim()) ) {
			//TODO
		}
		
	}
	
	@Test
	public void test8() {
		String val = null;
		System.out.println( "".equals(Optional.ofNullable("").orElse("").trim()) ); //true
		System.out.println( "".equals(Optional.ofNullable("   ").orElse("").trim()) );//true
		System.out.println( "".equals(Optional.ofNullable(val).orElse("").trim()) );//true
	}
	
	
	public void setNumser(String dept) {
		System.out.println( "".equals(Optional.ofNullable(dept).orElse("").trim()) );
	}
	
	@Test
	public void test9() {
		Long recordId = null;
		Optional.ofNullable(recordId).ifPresent(id->{
			id = 9L;
		});
//		System.out.println(Optional.ofNullable(recordId).ifPresent(id->{}));
	}
	
	@Test
	public void test10() {
		Map<String,Object> param1 = null;
		Map<String, Object> param = new HashMap<>();
		param.put("KHLX", 175);
		param.put("JCJB", 3);
		param.put("BEGINDATE", "2021-09-11");
		param.put("OVERDATE", "2021-10-01");
		
		Map<String,Object> op = Optional.ofNullable(param1).orElseGet(()->new HashMap<>());
		System.out.println(op);
		
	}
	
	
	@Test
	public void test11() {
		ReasonBO reason = new ReasonBO();
//		reason.setDetailName("明细。。");
		String detailName = Optional.ofNullable(reason)
				.map(ReasonBO::getDetailName)
				.orElseGet(()->null);
		System.out.println(detailName);
	}
	
	@Test
	public void test12() {
		List<ReasonBO> list = new ArrayList<>();
		ReasonBO reason = new ReasonBO();
		reason.setDetailName("明细");
		list.add(reason);
		
		String result = list.stream().filter(t->"明细2".equals(t.getDetailName()))
		.findAny()
		.map(u->u.getDetailName())
		.orElseGet(()->"无数据...");
		
		System.out.println(result);
	}
	
	@Test
	public void test14() {
		Double deduce = 2.2;
//		Double deduce = null;
		
		String res = Optional.ofNullable(deduce).map(t->{
			String _deduct = t.doubleValue()==t.intValue() ? String.valueOf(-t.intValue()) : String.valueOf(-t.doubleValue());
			return _deduct;
		}).orElseGet(()->"");
		
		System.out.println(res);
		
	}
	
	@Test
	public void test15() {
		List<Integer> list = null;
		Optional.ofNullable(list).orElseGet(()->Collections.emptyList()).add(2);
	}
	
	@Test
	public void test16() {
		List<ReasonBO> list = 			new ArrayList<>();
		Optional.ofNullable(list).map(t->t.get(0)).map(t->t.getDetailName()).orElseGet(()->""); // exception
	}
	
	@Test
	public void testMap() {
//		List<Map<Integer, Short>> list = new ArrayList<>();
//		Short res = Optional.ofNullable(list).filter(t->!t.isEmpty()).map(u->u.get(0)).map(u->u.get(4)).orElseGet(() -> (short) 999);
//		System.out.println(res); // 999
		
		List<Map<Integer, String>> list = new ArrayList<>();
		String res = Optional.ofNullable(list).filter(t->!t.isEmpty())
				.map(u->u.get(0))
				.map(u->u.get(4))
				.orElseGet(() -> "无");
		System.out.println(res); // 
	}
	
}
